#include<stdio.h>
#include<sys/mman.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>

int main(int argc, char** argv){
	if (argc != 2) {
		printf("Usage: ./read filedir\n");
		return -1;
	}
	char *dir = argv[1];
	int ret;

	int fd = open(dir, O_RDWR | O_CREAT, 0755);
	if (-1 == fd) {
		perror("open error");
		return -1;
	}
	ftruncate(fd, 1024);

	char *mm = (char*)mmap(0, 1024, PROT_READ, MAP_SHARED, fd, 0);
	if (MAP_FAILED == mm) {
		perror("mmap error");
		close(fd);
		return errno;
	}
	close(fd);
	int idx = 0;
	while(idx ++ < 10) {
		sleep(1);
		printf("%s\n", mm);
	}

	ret = munmap(mm, 1024);
	if (-1 == ret) {
		perror("munmap error");
		return errno;
	}

	return 0;
}
